package com.example.attendancesystem_1_8;


import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ImageButton;
import android.widget.TextView;

import androidx.fragment.app.Fragment;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationListener;

//import android.support.v4.app.Fragment;


//即WechatFragment
public class DakaFragment extends Fragment
{
    //打卡界面的控件
    private Button button_face;
    private Button button_location;
    private ImageButton imageButton_normal;
    private ImageButton imageButton_outside;
    private CheckBox checkBox_face;
    private CheckBox checkBox_location;

    //定位
    private Button btClientSingle;
    private TextView tvResultSingle;
    private AMapLocationClient locationClientSingle = null;


    public static final int TAKE_POTHO=1;
    private Uri uri;

    public DakaFragment()
    {

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        return inflater.inflate(R.layout.tab01, container, false);
    }

    //用onActivityCreated函数实现，用getActivity()获取当前Activity
    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        //寻找按钮
        button_face =(Button) getActivity().findViewById(R.id.button_Face);
        button_location = getActivity().findViewById(R.id.button_Location);
        imageButton_normal = getActivity().findViewById(R.id.imageButton_Normal);
        imageButton_outside = getActivity().findViewById(R.id.imageButton_Outside);
        checkBox_face = getActivity().findViewById(R.id.checkBox_Face);
        //checkBox_location = getActivity().findViewById(R.id.checkBox_Location);

        button_face.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v)
            {
                //checkBox_face.setChecked(true);
                Intent intent = new Intent(getActivity(), Test_Face.class);
                startActivity(intent);
            }


        });
        /*
        button_location.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View view)
            {
                //checkBox_location.setChecked(true);
            }
        });

         */
        imageButton_normal.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(checkBox_face.isChecked() && checkBox_location.isChecked())
                {
                    checkBox_face.setChecked(false);
                    checkBox_location.setChecked(false);
                }
            }
        });
        imageButton_outside.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(checkBox_face.isChecked() && checkBox_location.isChecked())
                {
                    checkBox_face.setChecked(false);
                    checkBox_location.setChecked(false);
                }
            }
        });

        btClientSingle = getActivity().findViewById(R.id.button_Location);

        tvResultSingle = getActivity().findViewById(R.id.tv_result1);

        btClientSingle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startSingleLocation();
            }
        });
    }





    // 单次定位
    public void singleLocation(View view) {
        if (btClientSingle.getText().equals(
                getResources().getString(R.string.startLocation))) {
            startSingleLocation();
            btClientSingle.setText(R.string.stopLocation);
            tvResultSingle.setText("正在定位...");
        } else {
            stopSingleLocation();
            btClientSingle.setText(R.string.startLocation);
        }
    }



    /**
     * 启动单次客户端定位
     */
    void startSingleLocation(){
        if(null == locationClientSingle){
            locationClientSingle = new AMapLocationClient(getActivity().getApplicationContext());
        }

        AMapLocationClientOption locationClientOption = new AMapLocationClientOption();
        //使用单次定位
        locationClientOption.setOnceLocation(true);
        // 地址信息
        locationClientOption.setNeedAddress(true);
        locationClientOption.setLocationCacheEnable(false);
        locationClientSingle.setLocationOption(locationClientOption);
        locationClientSingle.setLocationListener(locationSingleListener);
        locationClientSingle.startLocation();
    }

    /**
     * 停止单次客户端
     */
    void stopSingleLocation(){
        if(null != locationClientSingle){
            locationClientSingle.stopLocation();
        }
    }

    /**
     * 单次客户端的定位监听
     */
    AMapLocationListener locationSingleListener = new AMapLocationListener() {
        @Override
        public void onLocationChanged(AMapLocation location) {
            //long callBackTime = System.currentTimeMillis();
            StringBuffer sb = new StringBuffer();
            //sb.append("单次定位完成\n");
            //sb.append("回调时间: " + Utils.formatUTC(callBackTime, null) + "\n");
            if(null == location){
                sb.append("定位失败：location");
            } else {
                sb.append(Utils.getLocationStr(location));
            }
            tvResultSingle.setText("地址:"+sb.toString());
        }
    };



    @Override
    public void onDestroy() {
        super.onDestroy();
        if(null != locationClientSingle){
            locationClientSingle.onDestroy();
            locationClientSingle = null;
        }
    }


}
